import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

class NGuessNumberTest {
NGuessNumber ngn;
    @org.junit.jupiter.api.BeforeEach
    void setUp() {
        ngn = new NGuessNumber();
    }
    boolean isLegalAnswers(ArrayList<Integer> answers){
        //检查是否有⼩于0⼤于9的数或者是否有重复的数字，如果有就返回false
        for(int i = 0; i < answers.size(); i++){
            if(answers.get(i) < 0 || answers.get(i) > 9) {
                return false;
            }
            for(int j = i+1; j < answers.size(); j++){
                if(answers.get(i) == answers.get(j)){
                    return false;
                }
            }
        }
        return true;
    }

    @org.junit.jupiter.api.Test
    void generateAnswer() {
        ArrayList<Integer> answers = NGuessNumber.generateAnswer();

    assertEquals(4, answers.size()); //断⾔answers列表的⻓度是否和预期4⼀样，⼀样则测试通过，否则测试未通过
    assertTrue(isLegalAnswers(answers)); //断⾔isLegalAnswers⽅法返回的是否是真，是则测试通过，否则测试未通过
    }

    @org.junit.jupiter.api.Test
    void getA() {
        NGuessNumber ngn = new NGuessNumber();
        Integer [] answers = {1,2,3,4};
        ngn.answers = new ArrayList<Integer>(Arrays.asList(answers));
        Integer [] guesses = {5,6,7,8};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses));
        assertEquals(0, ngn.getA());
        Integer [] guesses1 = {1,2,3,4};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses1));
        assertEquals(4, ngn.getA());
        Integer [] guesses2 = {0,2,5,6};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses2));
        assertEquals(1, ngn.getA());
        Integer [] guesses3 = {0,2,3,6};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses3));
        assertEquals(2, ngn.getA());
        Integer [] guesses4 = {0,2,3,4};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses4));
        assertEquals(3, ngn.getA());

    }

    @org.junit.jupiter.api.Test
    void getB() {
        NGuessNumber ngn = new NGuessNumber();
        Integer [] answers = {1,2,3,4};
        ngn.answers = new ArrayList<Integer>(Arrays.asList(answers));
        Integer [] guesses = {5,6,7,8};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses));
        assertEquals(0, ngn.getB());
        Integer [] guesses1 = {4,3,2,1};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses1));
        assertEquals(4, ngn.getB());
        Integer [] guesses2 = {2,0,5,6};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses2));
        assertEquals(1, ngn.getB());
        Integer [] guesses3 = {2,1,5,6};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses3));
        assertEquals(2, ngn.getB());
        Integer [] guesses4 = {2,1,4,5};
        ngn.guesses = new ArrayList<Integer>(Arrays.asList(guesses4));
        assertEquals(3, ngn.getB());
    }
}